<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Config;
use think\Image;

class AcomController extends Controller
{
    private $table = '';
    private $full_table = '';
    private $uid;
    public function initialize()
    {
        $this->pre = config('database.prefix');
        $this->table = strtolower(request()->controller());
        $this->full_table = $this->pre.$this->table;
        if(empty(session('uid'))){
            $this->redirect('user/login');
        }else{
            $this->uid = session('uid');
        }
    }
    //列表
    public function Page_list(){
        $p=input('get.page/d',1);
        if(method_exists(model($this->table),'_list')){
           $list =  model($this->table)->_list($p);
        }else{
            $info = Db::table($this->full_table)->paginate('10','',['type'=>'bootstrap','page'=>$p]);
            $list['list'] = $info;
            $list['page'] = $info->render();
        }
        $this->assign('list',$list);
        return view();
    }
    //显示增/改页面
    public function create(Request $request)
    {
        $id = (input('get.id/d',''));
        if(method_exists(model($this->table),'_create')){
            $content = model($this->table)->__create($id);
        }else {
            if ($id) {//修改
                $content = Db::table($this->full_table)->find($id);
            } else {//添加
                $content = '0';
            }
        }
        $this->assign('content',json_encode($content));
        return view();
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete()
    {
        $id =input('get.id/d','');
        Db::table($this->full_table)->delete($id)? ajaxReturn('',1):ajaxReturn('',0);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request )
    {
        $data = $request->post();
        if(method_exists(model($this->table),'_save')){
             $re =  model($this->table)->_save($data);
        }else{
            if(intval($data['id'] > 0)){
                $re = Db::table($this->full_table)->where('id',$data['id'])->update($data);
            }else{
                $data['create_at'] = time();
                $re = model($this->table)->insert($data);
            }
        }
        if($re){
            $this->success('成功',url('page_list'));
        }else{
            $this->error('失败',url('page_list'));
        }
    }

    //上传文件
    public function uploads(Request $request){
        $files = $request->file('file');
        $ids = '';
        $data = [];
        foreach ($files as $file)
        {
           $img  = $file->validate(['ext'=>'jpg,png,gif'])->move('public/upload/images');
           if($img){
               $save_name = $img->getSaveName();
               $file_name = $img->getFilename();
               $thumb = Image::open($img);
               $thumb_path = 'public/upload/thumb/'.date('Ym').'/'.date('d'.'/');
               md($thumb_path);//没有得创建目录
               $thumb->thumb(250, 250)->save($thumb_path.$file_name);
               $thum = [
                   'file_name' => $img->getFilename(),
                   'save_name' =>$save_name,
                   'extension'=>$img->getExtension(),
                   'thumb_path'=>$thumb_path,
                   'create_at' => time(),
                   'update_at' => time(),
               ];
               $id = Db::table('yu_file_infos')->insertGetId($thum);
               $ids .=$id.',';
               $data['save_name'][] = $save_name;
           }else{
               ajaxReturn($file->getError());
           }
        }

        if($data['ids'] = trim($ids,',')){
            ajaxReturn('上传成功',1,$data);//返回
        }else{
            ajaxReturn();
        }
    }
}
